import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class Picture {

	private static int colorRange = 10;
	private static int[] colorRanges = new int[] { colorRange, colorRange, colorRange };

	public static BufferedImage convert(BufferedImage image) {
		ImageIcon imageIcon = new ImageIcon(image);
		BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
		Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
		g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());

		int ground = bufferedImage.getRGB(bufferedImage.getMinX(), bufferedImage.getMinY());
		for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
			for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
				int rgb = bufferedImage.getRGB(j2, j1);
				if (colorInRange(rgb, ground)) {

					rgb = ground;
				} else {

				}
				bufferedImage.setRGB(j2, j1, rgb);
			}
		}
		g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
		return bufferedImage;
	}

	/**
	 * 颜色差
	 * 
	 * @param color
	 * @param ground
	 * @return
	 */
	public static boolean colorInRange(int color, int ground) {
		if (Math.abs(new Color(color).getRed() - new Color(ground).getRed()) > colorRanges[0]) {
			return false;
		}
		if (Math.abs(new Color(color).getGreen() - new Color(ground).getGreen()) > colorRanges[1]) {
			return false;
		}
		if (Math.abs(new Color(color).getBlue() - new Color(ground).getBlue()) > colorRanges[2]) {
			return false;
		}
		return true;
	}

}